package com.china08.yxyapi.api.manage.tec.teaching.lecture.catalog;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.entity.db.res.YxyResBook;
import com.china08.yxyapi.entity.mg.lecture.YxyLectureCatalog;
import com.china08.yxyapi.model.YxyApiReqModel;
import com.china08.yxyapi.model.YxyApiRespModel;
import com.china08.yxyapi.repository.db.YxyResBookRepository;
import com.china08.yxyapi.repository.mg.lecture.YxyLectureCatalogRepository;

import io.swagger.annotations.ApiOperation;

/**
 * 老师 ---备授课--添加备授课目录
 * 
 */
@CrossOrigin
@RestController
public class LectureCrtCatalogController extends YxyApiBaseController {

    @Autowired
    private YxyLectureCatalogRepository yxyLectureCatalogRepository;

    @Autowired
    private YxyResBookRepository yrbr;

    @ApiOperation(value = "老师添加备授课目录")
    @RequestMapping(value = "/api_yw/manage/tec/teaching/lecture/catalog/crt", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public Object process(HttpServletRequest request, HttpServletResponse response, @RequestBody ReqModel reqModel) {

        String userId = getProfile(request, response).getId();
        String schoolId = getProfile(request, response).getSchoolId();

        // ### 查询书籍信息
        Assert.notNull(StringUtils.defaultIfBlank(userId, null), "用户id必须");
        Assert.notNull(StringUtils.defaultIfBlank(schoolId, null), "学校id必须");
        String bookId = reqModel.bookId;
        YxyResBook yxyResBook = yrbr.findById(bookId);
        Assert.notNull(yxyResBook, "找不到书籍，请刷新页面");
        // ### 书籍信息
        String subjectId = yxyResBook.getSubjectId();
        String subjectName = yxyResBook.getSubjectName();
        String versionId = yxyResBook.getVersionId();
        String versionName = yxyResBook.getVersionName();
        String semesterId = yxyResBook.getSemersterId();
        String semesterName = yxyResBook.getSemersterName();
        String phaseId = yxyResBook.getPhaseId();
        String phaseName = yxyResBook.getPhaseName();

        // ### 根据书籍信息判断是否存在
        long check = yxyLectureCatalogRepository.countByUserIdAndSchoolIdAndBookId(userId, schoolId, bookId);
        Assert.isTrue(check == 0, "备课目录已经存在");
        // ### 添加备授课目录
        YxyLectureCatalog yxyLectureCatalog = new YxyLectureCatalog();
        yxyLectureCatalog.setUserId(userId);
        yxyLectureCatalog.setSchoolId(schoolId);
        yxyLectureCatalog.setPhaseId(phaseId);
        yxyLectureCatalog.setPhaseName(phaseName);
        yxyLectureCatalog.setSubjectId(subjectId);
        yxyLectureCatalog.setSubjectName(subjectName);
        yxyLectureCatalog.setVersionId(versionId);
        yxyLectureCatalog.setVersionName(versionName);
        yxyLectureCatalog.setSemesterId(semesterId);
        yxyLectureCatalog.setSemesterName(semesterName);
        yxyLectureCatalog.setBookId(bookId);
        // ** 改为0
        List<YxyLectureCatalog> catalogs = yxyLectureCatalogRepository.findByUserIdAndSchoolIdAndIsDefault(userId,
                schoolId, 1);
        for (YxyLectureCatalog yxyLectureCatalog2 : catalogs) {
            yxyLectureCatalog2.setIsDefault(0);
            yxyLectureCatalogRepository.save(yxyLectureCatalog2);
        }
        // 判断是否是第一个创建
        // 如果是第一个默认就是缺省字段 （缺省目录）
        yxyLectureCatalog.setIsDefault(1);
        yxyLectureCatalog = yxyLectureCatalogRepository.save(yxyLectureCatalog);

        // ### model 封装
        RespModel respModel = new RespModel();
        respModel.bookId = bookId;
        respModel.phaseName = phaseName;
        respModel.subjectName = subjectName;
        respModel.versionName = versionName;
        respModel.semesterName = semesterName;
        respModel.isDefault = yxyLectureCatalog.getIsDefault();
        respModel.id = yxyLectureCatalog.getId();
        respModel.phaseId = yxyLectureCatalog.getPhaseId();
        respModel.subjectId = yxyLectureCatalog.getSubjectId();
        respModel.setMessage("添加备课目录成功");
        return respModel;
    }

    protected static class ReqModel extends YxyApiReqModel {

        /**
         * 书籍ID
         */
        public String bookId;

        public void check() {
            Assert.notNull(StringUtils.defaultIfBlank(bookId, null), "学段id不能为空");
        }

    }

    protected static class RespModel extends YxyApiRespModel {
        public String id;
        public String bookId;

        public String phaseName;

        public String subjectName;

        public String versionName;

        public String semesterName;

        public Integer isDefault;

        public String phaseId;

        public String subjectId;
    }
}
